<html>
<head>
<title>Web Client Dummy</title>
<script src="rpc.js" type="text/javascript"></script>
<script src="common.pbjson.js" type="text/javascript"></script>
<script src="waveclient-rpc.pbjson.js" type="text/javascript"></script>
<script src="webclient.pbjson.js" type="text/javascript"></script>
<script src="jsot.js" type="text/javascript"></script>
</head>
<body>
	<h1>Log-In</h1>
	
	<form>
	  <p>User JID: <input type="text" id="jid" value="tux@wave1.vs.uni-due.de" /></p>
	  <p><input type="button" value="Login" onclick="JSOT.Rpc.login( document.getElementById('jid').value )" /></p>
	  <p><input type="button" value="NewWave" onclick="newWave()" /></p>
	  <p><textarea id="comment"></textarea><br><input type="button" value="Submit" onclick="submitBlip()" /> Share your thoughts<p>
	  <p>Add a friend: <input type="text" id="friend" value="torben@wave1.vs.uni-due.de" /> <input type="button" value="Add" onclick="addFriend()" /></p>
	  <p><input type="button" value="BruteForce" onclick="bruteForce()" /> <input type="button" value="Open" onclick="openWave()" /></p>
	</form>

	<h3>Session ID</h3>
	<p id="sessionId">No session ID yet</p>

	<h3>Protocol</h3>
	<div id="out"></div>

	<script type="text/javascript">
	
/*	
var sessionId = "";
var clientSequenceNr = 0;
var serverSequenceNr = 0;
var serverAck = 0;
*/

function login()
{
	JSOT.Rpc.login( document.getElementById('jid').value );
}

/*
function toArray(str)
{
  result = [];
  for( var i = 0; i < str.length; ++i )
  {
	result.push( str.charCodeAt(i) );
  }
  return result;
}
*/

function newWave()
{
	// var waveid = "w+" + Math.random().toString();
	// var waveid = "wall+" + JSOT.Rpc.jid;
	var waveid = "wall+all";
  
	var wave = JSOT.Wave.getWave( waveid, "wave1.vs.uni-due.de" );
	var wavelet = wave.getWavelet( "conv+root", "wave1.vs.uni-due.de" );

	var mutations = [];
	var m1 = new protocol.ProtocolWaveletOperation_MutateDocument();
	m1.document_id = "conversation";
	m1.document_operation = new protocol.ProtocolDocumentOperation();
	var docop = m1.document_operation;
	docop.component.push( protocol.ProtocolDocumentOperation.newElementStart( "conversation" ) );
	docop.component.push( protocol.ProtocolDocumentOperation.newElementStart( "blip", [ protocol.ProtocolDocumentOperation.newKeyValuePair( "id", "b+1" ) ] ) );
	docop.component.push( protocol.ProtocolDocumentOperation.newElementEnd() );
	docop.component.push( protocol.ProtocolDocumentOperation.newElementEnd() );
	mutations.push( m1 );
	
	var m2 = new protocol.ProtocolWaveletOperation_MutateDocument();
	m2.document_id = "b+1";
	m2.document_operation = new protocol.ProtocolDocumentOperation();
	var docop = m2.document_operation;
	docop.component.push( protocol.ProtocolDocumentOperation.newElementStart( "body" ) );
	docop.component.push( protocol.ProtocolDocumentOperation.newElementStart( "line" ) );
	docop.component.push( protocol.ProtocolDocumentOperation.newCharacters( "Hallo" ) );
	docop.component.push( protocol.ProtocolDocumentOperation.newElementEnd() );
	docop.component.push( protocol.ProtocolDocumentOperation.newElementEnd() );
	mutations.push( m2 );
	
	wavelet.submitMutations( mutations, JSOT.Rpc.jid, true );
}

function openWave()
{
	JSOT.Rpc.openWavelet( "wave://wave1.vs.uni-due.de/wall+all/conv+root" );
}

function submitBlip()
{
  var waveid = "wall+" + JSOT.Rpc.jid;
  var blipid = "b+" + Math.random();
  
  var wave = JSOT.Wave.getWave( waveid, "wave1.vs.uni-due.de" );
  var wavelet = wave.getWavelet( "conv+root", "wave1.vs.uni-due.de" );

  var mutations = [];
  var m1 = new protocol.ProtocolWaveletOperation_MutateDocument();
  m1.document_id = "conversation";
  m1.document_operation = new protocol.ProtocolDocumentOperation();
  var docop = m1.document_operation;
  docop.component.push( protocol.ProtocolDocumentOperation.newRetainItemCount(1) );
  docop.component.push( protocol.ProtocolDocumentOperation.newElementStart( "blip", [ protocol.ProtocolDocumentOperation.newKeyValuePair( "id", blipid ) ] ) );
  docop.component.push( protocol.ProtocolDocumentOperation.newElementEnd() );
  docop.component.push( protocol.ProtocolDocumentOperation.newRetainItemCount( wavelet.getDoc("conversation").itemCount() - 1 ) );
  mutations.push(m1);
  
  var m2 = new protocol.ProtocolWaveletOperation_MutateDocument();
  m2.document_id = blipid;
  m2.document_operation = new protocol.ProtocolDocumentOperation();
  var docop = m2.document_operation;
  docop.component.push( protocol.ProtocolDocumentOperation.newElementStart( "body" ) );
  docop.component.push( protocol.ProtocolDocumentOperation.newElementStart( "line" ) );
  docop.component.push( protocol.ProtocolDocumentOperation.newElementEnd() );
  docop.component.push( protocol.ProtocolDocumentOperation.newCharacters( document.getElementById('comment').value ) );
  docop.component.push( protocol.ProtocolDocumentOperation.newElementEnd() );
  mutations.push(m2);
  
  wavelet.submitMutations( mutations );
}

function addFriend()
{
  var waveid = "wall+all";
//  var waveid = "wall+" + JSOT.Rpc.jid;
  var friend = document.getElementById('friend').value;
  
  var wave = JSOT.Wave.getWave( waveid, "wave1.vs.uni-due.de" );
  var wavelet = wave.getWavelet( "conv+root", "wave1.vs.uni-due.de" );
  
  wavelet.submitAddParticipant( friend );
}

function bruteForce()
{
//  var waveid = "wall+" + JSOT.Rpc.jid;
  var waveid = "wall+all";
  
  var wave = JSOT.Wave.getWave( waveid, "wave1.vs.uni-due.de" );
  var wavelet = wave.getWavelet( "conv+root", "wave1.vs.uni-due.de" );
  var doc = wavelet.getDoc( "b+1");
  
  for( var i = 0; i < 100; ++i )
  {
	var mutations = [];
  	
	var m2 = new protocol.ProtocolWaveletOperation_MutateDocument();
	m2.document_id = "b+1";
	m2.document_operation = new protocol.ProtocolDocumentOperation();
	var docop = m2.document_operation;
	docop.component.push( protocol.ProtocolDocumentOperation.newRetainItemCount( 3 ) );
	docop.component.push( protocol.ProtocolDocumentOperation.newCharacters( i.toString() + "," ) );
	docop.component.push( protocol.ProtocolDocumentOperation.newRetainItemCount( doc.itemCount() - 3 ) );
	mutations.push(m2);
  
	wavelet.submitMutations( mutations );
  }
}

	</script>
</body>
</html>	